package org.xx.armory.docs;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 文档。
 *
 * @author Haart
 * @see Block
 * @see Element
 */
public final class Document implements Collection<Block>, Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    private final List<Block> blocks;

    /**
     * 构造文档实例。
     */
    public Document() {
        this.blocks = new ArrayList<>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int size() {
        return this.blocks.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isEmpty() {
        return this.blocks.isEmpty();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean contains(Object o) {
        return this.blocks.contains(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Iterator<Block> iterator() {
        return this.blocks.iterator();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Object[] toArray() {
        return this.blocks.toArray();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("SuspiciousToArrayCall")
    public <T> T[] toArray(final T[] a) {
        return this.blocks.toArray(a);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean add(final Block e) {
        return this.blocks.add(e);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean remove(final Object o) {
        return this.blocks.remove(o);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean containsAll(final Collection<?> c) {
        return this.blocks.containsAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean addAll(final Collection<? extends Block> c) {
        return this.blocks.addAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean removeAll(final Collection<?> c) {
        return this.blocks.removeAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean retainAll(final Collection<?> c) {
        return this.blocks.retainAll(c);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void clear() {
        this.blocks.clear();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final String toString() {
        final StringBuilder sb = new StringBuilder();

        sb.append("<body>");
        for (final Block b : this.blocks) {
            sb.append(b).append("\n");
        }
        sb.append("</body>");

        return sb.toString();
    }
}
